<?php
class editAction extends sfActions
{
	public function execute($request)
	{
		$this->facility_id = $request->getParameter('facility_id');
		if ($this->facility_id) {
			$this->facility = Doctrine_Core::getTable('DtFacility')->find($this->facility_id);
			if ($this->facility)
				$this->facility_form = new AdminFacilityForm($this->facility);
		}
		
		if ($request->hasParameter('facility')) {
			$form = $request->getParameter('facility');
			$this->status = $this->updateFacilityFromRequest($this->facility, $form);
			if ($this->status) {
				$this->facility->save();
				$this->forward('facility', 'index');
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
		
	}
	
	
	private function updateFacilityFromRequest(&$facility, $form)
	{
		if ($form['name']) {
			$c = Doctrine_Core::getTable('DtFacility')->findOneByName($form['name']);
			if (!$c || ($c->name == $facility->name)) {
				$facility->name = $form['name'];
			} else {
				return false;
			}
		}
		
		if ($form['value'])
			$facility->value = $form['value'];

		$facility->updated_at = date(DATE_ISO8601);
		return true;
	}
}
?>